package server.util.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

public class EnvironmentVariablesTableModel extends AbstractTableModel {

    public static final int //
            COL_NAME = 0,
            COL_VALUE = 1;

    private String[] columnNames = {"Name", "Value"};
    private List<String> data;

    public EnvironmentVariablesTableModel() {
        super();

        data = new ArrayList<>();
    }

    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int row, int col) {
        String[] entry = data.get(row).split("<DELIMITER>");

        switch (col) {
            case COL_NAME:
                return entry[0];
            case COL_VALUE:
                return entry[1];
        }

        return "-";
    }

    @Override
    public String getColumnName(int col) {
        return columnNames[col];
    }

    public void load(Map<String, String> env) {
        for (String s : env.keySet()) {
            data.add(String.format("%s<DELIMITER>%s", s, env.get(s)));
        }
        
        this.fireTableDataChanged();
    }
    
    public void clear() {
        data.clear();
    }

}
